/*
 * Copyright (C) 2014 nohana, Inc.
 * Copyright 2017 Zhihu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lgmshare.component.mediapacker.internal.entity;

import android.content.pm.ActivityInfo;

import androidx.annotation.StyleRes;

import com.lgmshare.component.mediapacker.MimeType;
import com.lgmshare.component.mediapacker.R;
import com.lgmshare.component.mediapacker.engine.ImageEngine;
import com.lgmshare.component.mediapacker.engine.impl.GlideEngine;
import com.lgmshare.component.mediapacker.filter.Filter;

import java.util.List;
import java.util.Set;

public final class SelectionSpec {

    public Set<MimeType> mimeTypeSet;//类型集合
    public boolean mediaTypeExclusive;//类型排除
    public boolean mediaTypeShowSingle;//仅显示一种类型
    @StyleRes
    public int themeId;
    public int orientation;
    public boolean countable;//显示计数
    public boolean capture;//显示相机
    public CaptureStrategy captureStrategy;//拍照配置参数
    public int maxSelectable;//最大选择数
    public int maxImageSelectable;
    public int maxAudioSelectable;//音频最大选择数
    public int maxVideoSelectable;//视频最大选择数
    public List<Filter> filters;//设置过滤文件
    public ImageEngine imageEngine;
    public int spanCount;//每行个数
    public int gridExpectedSize;
    public int originalMaxSize;//原图最大大小
    public float thumbnailScale;
    public boolean inited;
    public boolean original;//原图
    public boolean autoHideToolbar;
    public boolean showPreview;//显示预览
    public List<AlbumMedia> selectedMedias;//已选中图片

    private SelectionSpec() {
    }

    public static SelectionSpec getInstance() {
        return InstanceHolder.INSTANCE;
    }

    public static SelectionSpec getCleanInstance() {
        SelectionSpec selectionSpec = getInstance();
        selectionSpec.reset();
        return selectionSpec;
    }

    private void reset() {
        mimeTypeSet = null;
        mediaTypeExclusive = true;
        mediaTypeShowSingle = false;
        themeId = R.style.MediaPicker_DefaultStyle;
        orientation = 0;
        countable = false;
        maxSelectable = 1;
        maxImageSelectable = 0;
        maxAudioSelectable = 0;
        maxVideoSelectable = 0;
        filters = null;
        capture = false;
        captureStrategy = null;
        spanCount = 3;
        gridExpectedSize = 0;
        thumbnailScale = 0.5f;
        imageEngine = new GlideEngine();
        inited = true;
        original = false;
        autoHideToolbar = false;
        originalMaxSize = Integer.MAX_VALUE;
        showPreview = true;
    }

    public boolean singleSelectionModeEnabled() {
        return !countable && (maxSelectable == 1 || (maxImageSelectable == 1 && maxVideoSelectable == 1 && maxAudioSelectable == 1));
    }

    public boolean needOrientationRestriction() {
        return orientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    }

    public boolean onlyShowGif() {
        return mediaTypeShowSingle && MimeType.ofGif().equals(mimeTypeSet);
    }

    public boolean onlyShowImages() {
        return mediaTypeShowSingle && MimeType.ofImage().containsAll(mimeTypeSet);
    }

    public boolean onlyShowAudios() {
        return mediaTypeShowSingle && MimeType.ofAudio().containsAll(mimeTypeSet);
    }

    public boolean onlyShowVideos() {
        return mediaTypeShowSingle && MimeType.ofVideo().containsAll(mimeTypeSet);
    }

    private static final class InstanceHolder {
        private static final SelectionSpec INSTANCE = new SelectionSpec();
    }
}
